<template>
  <div class="first-password-container">
    <a-alert message="首次登录请修改密码" banner closable />
    <div class="update-warp">
      <div class="title">修改密码</div>
      <a-form-model ref="ruleForm" :model="form" :rules="rules">
        <a-form-model-item label="当前账号">
          <a-input v-model="userInfo.userNameId" disabled></a-input>
        </a-form-model-item>
        <a-form-model-item label="原密码" prop="originalPassword">
          <a-input-password
            v-model.trim="form.originalPassword"
            placeholder="请输入"
          ></a-input-password>
        </a-form-model-item>
        <a-form-model-item label="新密码" prop="password">
          <a-input-password
            v-model.trim="form.password"
            placeholder="请输入"
          ></a-input-password>
        </a-form-model-item>
        <a-form-model-item label="新密码" prop="password2">
          <a-input-password
            v-model.trim="form.password2"
            placeholder="请输入"
          ></a-input-password>
        </a-form-model-item>
      </a-form-model>
      <a-button class="update-footer" @click="handleOk()" :loading="loading">
        确定
      </a-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      form: {
        //原密码
        originalPassword: "",
        //新密码
        password: "",
        //新密码
        password2: "",
      },
      rules: {
        originalPassword: [
          {
            required: true,
            message: "请输入原密码",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入新密码",
          },
          {
            min: 1,
            maxLength: 11,
            pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,16}$/,
            message:
              "必须有数字和字母组合、包含大小写字母、包含特殊字符的8到16位长度密码",
          },
        ],
        password2: [
          {
            required: true,
            message: "请输入新密码",
          },
          {
            min: 1,
            maxLength: 11,
            pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,16}$/,
            message:
              "必须有数字和字母组合、包含大小写字母、包含特殊字符的8到16位长度密码",
          },
        ],
      },
      loading: false,
    };
  },
  computed: {
    ...mapGetters(["nickname", "userInfo"]),
  },
  mounted() {},
  methods: {
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { userInfo, form } = this;
          if (form.password != form.password2) {
            this.$message.warning("新密码输入不一致！", 1.5);
            return;
          }
          this.loading = true;
          let formJson = {
            //用户id
            uuid: userInfo.uuid,
            //密码
            password: form.password,
          };
          this.$post("/mdata/user/batchResetPassword", formJson)
            .then((res) => {
              if (res.statusCode === 200) {
                this.$message.success(res.message + "请重新登录", 1.5);
                localStorage.clear();
                this.$router.push({
                  path: "/user/login?type=private",
                });
              } else {
                this.$message.error(res.message, 1.5);
              }
            })
            .finally((_) => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.first-password-container {
  height: 100%;
  width: 100%;
  position: relative;
  .update-warp {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 480px;
    // height: 478px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 4px;
    border: 1px solid #e3eeff;
    padding: 20px 50px 40px 50px;
    box-sizing: border-box;
    .title {
      text-align: center;
      font-family: Microsoft YaHei;
      font-weight: bold;
      font-size: 30px;
      color: #333333;
      padding-bottom: 20px;
      box-sizing: border-box;
      letter-spacing: 10px;
    }
    /deep/.ant-form {
      .ant-form-item {
        display: flex;
        .ant-form-item-label {
          flex-shrink: 0;
          width: 60px;
        }
        .ant-form-item-control-wrapper {
          flex: 1;
          overflow: hidden;
        }
      }
    }
    .update-footer {
      margin-top: 20px;
      width: 100%;
      height: 48px;
      background: #1677ff;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: bold;
      color: #ffffff;
      padding: 0;
      border: none;
      &.ant-btn[disabled]:hover {
        background: rgba(22, 119, 255, 0.8);
        color: #ffffff;
      }
    }
  }
}
</style>
